'use strict';

const Controller = require('egg').Controller;

class NewsController extends Controller {
  async list() {
    // const dataList = {
    //   list: [
    //     { id: 1, title: 'this is news 1', url: '/news/1' },
    //     { id: 2, title: 'this is news 2', url: '/news/2' },
    //   ],
    // };


    const dataList = {
      list: [],
      user: {},
    };
    dataList.list = await this.ctx.model.City.find({ }, function(err, citys) {
      if (err) {
        return console.log('error:' + err);
      }
      if (citys.length > 0) {
        const city = citys[0];
        console.log('%s %s is a %s.', city.name, city.country, city.zipCode);
      }
    });
    const users = await this.ctx.model.User.find({ });
    dataList.user = users[0];
    if (dataList.user.length <= 0) {
      const User = this.ctx.model.User;
      const user = new User({ name: 'Zildjian', uid: '10000002', password: '111111' });

      // 调用 .save 方法后，mongoose 会去你的 mongodb 中的 test 数据库里，存入一条记录。
      user.save(function(err) {
        if (err) {
          console.log('meow');
        }
      });
    }
    await this.ctx.render('news/login.tpl', dataList);


  }
  // async list() {
  //   const { ctx, app } = this;
  //   const pageSize = app.config.news.pageSize;
  //   const page = parseInt(ctx.query.page) || 1;

  //   const idList = await ctx.service.hackerNews.getTopStories(page);

  //   // get itemInfo parallel
  //   const newsList = await Promise.all(idList.map(id => ctx.service.hackerNews.getItem(id)));
  //   await ctx.render('news/list.tpl', { list: newsList, page, pageSize });
  // }

  // async detail() {
  //   const { ctx } = this;
  //   const id = ctx.params.id;
  //   const newsInfo = await ctx.service.hackerNews.getItem(id);
  //   // get comment parallel
  //   const commentList = await Promise.all(newsInfo.kids.map(id => ctx.service.hackerNews.getItem(id)));
  //   await ctx.render('news/detail.tpl', { item: newsInfo, comments: commentList });
  // }

  // async user() {
  //   const { ctx } = this;
  //   const id = ctx.params.id;
  //   const userInfo = await ctx.service.hackerNews.getUser(id);
  //   await ctx.render('news/user.tpl', { user: userInfo });
  // }
}

module.exports = NewsController;
